###
#Code for descriptive portions of analysis
###
library(plyr)

load('ownership.staff.frame.RData')
write.csv(ownership.staff.frame,file='ownership.staff.frame.csv',row.names=FALSE)

#Investment Chain Types in 2022
table(ownership.staff.frame$chain.grouping[which(ownership.staff.frame$year==2022 & ownership.staff.frame$owner.type=="Investment")])

#Figure 1
ownership.staff.frame$public <- ifelse(ownership.staff.frame$owner.type=="Public",1,0)
ownership.staff.frame$investment <- ifelse(ownership.staff.frame$owner.type=="Investment",1,0)

staff.plot.2005 <- ddply(ownership.staff.frame,.(year),summarise,average.reporters=mean(reporters.editors,na.rm=TRUE),investment=mean(investment,na.rm=TRUE),public=mean(public,na.rm=TRUE),entertainment.reporters=mean(entertainment.reporters.editors,na.rm=TRUE))
staff.plot.2005$private <- 1 - (staff.plot.2005$investment + staff.plot.2005$public)

pdf(height=4,width=8.5,file='figure1.pdf')
par(mfrow=c(1,2),mar=c(4,4,2,1))

#Panel A of Figure 1
plot(staff.plot.2005$year,staff.plot.2005$average.reporters,type='l',lwd=4,las=1,xlab='Year',main='Newsroom Size',ylim=c(0,85),ylab='Average No. Reporters and Editors')

#Panel B of Figure 1
plot(x=staff.plot.2005$year,y=staff.plot.2005$investment,ylim=c(.035,.965),lwd=3,xlim=c(2005.55,2021.45),las=1,main='Ownership',xlab='Year',type='n',ylab='Ownership by Category',yaxt='n',bty='n')
polygon(x=c(2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022,2022,2005),y=c(staff.plot.2005$investment,0,0),lwd=2,col='gray50',border='gray50')
polygon(x=c(2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022,rev(c(2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022))),y=c(staff.plot.2005$investment+staff.plot.2005$public,rev(staff.plot.2005$investment)),lwd=2,col='gray85',border='gray85')
text(x=2014,y=.0525,label='Investment',cex=1)
text(x=2014,y=.36,label='Public',cex=1)
text(x=2014,y=.775,label='Private',cex=1)
axis(side=2,at=c(0,.2,.4,.6,.8,1),labels=c('0%','20%','40%','60%','80%','100%'),las=1,cex.axis=1)
box(lwd=1)
dev.off()
